<template>
  <div class="wrapper">
    <div class="copy-info card">
      <div class="title">{{ appraisalData.deptNameTree }}</div>
      <div class="btn-wrap">
        <div class="time">
          {{ appraisalData.year }}年<span v-if="$route.query.type != 'year'">第{{ appraisalData.quarter }}季度</span>
        </div>
        <div class="btn-wrapper">
          <div class="btn" :class="{ active: active == 0 }" @click="active = 0">未评鉴</div>
          <div class="btn" :class="{ active: active == 1 }" @click="active = 1">已评鉴</div>
        </div>
      </div>
    </div>
    <pull-refresh-list ref="pageRecords" v-show="active == 0" :limit="queryForm.pageSize" :page-no.sync="queryForm.pageNo" :data.sync="list" @getList="initRecords">
      <div class="list-item card" v-for="item in list" :key="item.id">
        <user-avater :avater="item.avater" :userName="item.userName" />
        <div class="item-content">
          <div class="name flex">
            <div>{{ item.userName }}</div>
            <div class="flex suggest" v-if="item.adviseLevel && $route.query.type != 'year'">
              <div class="text">建议等次</div>
              <div class="num" style="margin-left:8px">{{ levelStatus[item.adviseLevel] }}</div>
            </div>
          </div>
          <div class="copy van-ellipsis">隶属单位：{{ item.orgName }}</div>
          <div class="copy" v-if="type == 2 && $route.query.type != 'year'">
            <span>单位排名：{{ item.rank }}</span
            >&nbsp;&nbsp;<span>考核得分：{{ item.finalPoint }}</span>
          </div>
        </div>
        <div class="btn" v-if="$route.query.type != 'year'" :class="{ middle: !item.adviseLevel }" @click="linkDetail(item)">{{ type == 1 ? "建议" : "评定" }}等次</div>
        <div class="right " :class="{ haspadding: type == 2 }" v-else>
          <div class=" suggest" v-if="type == 2">
            <span class="text">建议等次：<span v-if="!item.adviseLevel">暂无</span></span>
            <span class="num">{{ levelStatus[item.adviseLevel] }}</span>
          </div>
          <div class="btn" @click="linkDetail(item)">{{ type == 1 ? "建议等次" : "评定等次" }}</div>
        </div>
      </div>
    </pull-refresh-list>
    <pull-refresh-list ref="pageFinishedRecords" v-show="active == 1" :limit="finishQueryForm.pageSize" :page-no.sync="finishQueryForm.pageNo" :data.sync="finishedList" @getList="initFinishedRecords">
      <div class="list-item card" v-for="item in finishedList" :key="item.id" @click="linkDetail(item)">
        <user-avater :avater="item.avater" :userName="item.userName" />
        <div class="item-content">
          <div class="name">{{ item.userName }}</div>
          <div class="copy van-ellipsis">隶属单位：{{ item.orgName }}</div>
          <div class="copy" v-if="$route.query.type != 'year'">
            <span>单位排名：{{ item.rank }}</span
            >&nbsp;&nbsp;<span>考核得分：{{ item.finalPoint }}</span>
          </div>
        </div>
        <div class="score">
          <div class="num">{{ type == 1 ? levelStatus[item.adviseLevel] : levelStatus[item.judgeLevel] }}</div>
          <div class="text" v-if="type == 1">建议等次</div>
        </div>
      </div>
    </pull-refresh-list>
  </div>
</template>

<script>
import { getEvaluationList, getYearEvaluationList } from "@/api/appraisalProcess";
import { getDictionaryByType } from "@/api/common";
export default {
  props: {
    type: {
      type: Number,
      default: 1
    },
    appraisalData: {
      type: Object,
      default: () => {
        return {};
      }
    },
    finished: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      active: 0,
      queryForm: {
        pageSize: 10,
        pageNo: 1,
        evaluationStatus: 0
      },
      finishQueryForm: {
        pageSize: 10,
        pageNo: 1,
        evaluationStatus: 1
      },
      list: [],
      finishedList: [],
      levelStatus: {}
    };
  },
  watch: {
    active(val) {
      this.onRefresh();
    }
  },
  created() {
    this.active = this.finished ? 1 : 0;
    let form = {
      deptId: this.appraisalData.deptId,
      year: this.appraisalData.year,
      evaluationType: this.type == 1 ? "adviseLevel" : "judgeLevel"
    };
    if (this.$route.query.type != "year") {
      form.quarter = this.appraisalData.quarter;
    }
    Object.assign(this.queryForm, form);
    Object.assign(this.finishQueryForm, form);
    this.getLevel();
  },
  methods: {
    getLevel() {
      let type = this.$route.query.type == "year" ? "yearAssess" : "quarterAssess";
      this.levelStatus = {};
      getDictionaryByType(type).then(res => {
        res.data.forEach(ele => {
          this.$set(this.levelStatus, ele.dictValue, ele.dictLabel);
        });
      });
    },
    linkDetail(item) {
      sessionStorage.setItem("appraisalUser", JSON.stringify(item));
      this.$router.push({
        path: "detail",
        query: {
          type: this.type,
          status: this.active,
          id: item.id,
          uid: item.userId,
          is_year: this.$route.query.type == "year" ? "1" : "0",
          did: this.$route.query.did
        }
      });
    },
    onRefresh() {
      if (this.active == 0) {
        this.$refs["pageRecords"].onSearch();
      } else {
        this.$refs["pageFinishedRecords"].onSearch();
      }
    },
    initRecords(callback) {
      let getListFn = getEvaluationList;
      if (this.$route.query.type == "year") {
        getListFn = getYearEvaluationList;
      }
      getListFn(this.queryForm).then(res => {
        callback(res.data.records);
      });
    },
    initFinishedRecords(callback) {
      let getListFn = this.$route.query.type == "year" ? getYearEvaluationList : getEvaluationList;
      getListFn(this.finishQueryForm).then(res => {
        callback(res.data.records);
      });
    }
  }
};
</script>

<style scoped lang="scss">
.wrapper {
  padding: 16px;
}
.card {
  background: #fff;
  border-radius: 8px;
  margin-bottom: 12px;
  padding: 16px 20px;
}
.copy-info {
  .title {
    font-size: 14px;
    line-height: 20px;
    margin-bottom: 3px;
  }
  .btn-wrap {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .time {
      color: #a3a7ad;
      font-size: 13px;
    }
    .btn-wrapper {
      display: flex;
      align-items: center;

      .btn {
        margin-left: 12px;
        line-height: 22px;
        background: rgba(204, 204, 204, 0.2);
        border-radius: 4px;
        padding: 0 6px;
        white-space: nowrap;
        text-align: center;
        font-size: 10px;
        color: #a3a7ad;
        &.active {
          color: #fff;
          background: #2492ff;
        }
      }
    }
  }
}
.list-item {
  position: relative;
  display: flex;
  align-items: center;

  .right {
    position: relative;
    &.haspadding {
      padding-top: 16px;
    }
    .suggest {
      width: 160px;
      text-align: right;
      right: 0;
      position: absolute;
      top: 0;
    }
    .btn {
      position: unset;
      margin: 6px 0 0;
    }
  }
  .suggest {
    font-size: 12px;
    .text {
      color: rgba(36, 46, 69, 0.3);
    }
    .num {
      color: #f60e36;
    }
  }
  .item-content {
    flex: 1;
    .name {
      justify-content: space-between;
      font-size: 14px;
      color: #333;
      line-height: 20px;
    }
    .copy {
      max-width: 190px;
      margin-top: 3px;
      color: #a3a7ad;
      font-size: 12px;
    }
  }
  .btn {
    position: absolute;
    bottom: 24px;
    right: 20px;
    width: 52px;
    white-space: nowrap;
    line-height: 24px;
    height: 24px;
    border-radius: 4px;
    color: #fff;
    text-align: center;
    margin-left: 12px;
    font-size: 10px;
    background: #2492ff;
    &.middle {
      top: 0;
      bottom: 0;
      margin: auto;
    }
  }
  .score {
    text-align: right;
    .num {
      text-align: right;
      font-size: 16px;
      font-weight: 600;
      color: #f60e36;
    }
    .text {
      margin-top: 4px;
      color: rgba(36, 46, 69, 0.3);
      font-size: 12px;
    }
  }
}
</style>
